package com.example.demo.response;


import com.alibaba.druid.util.StringUtils;

/**
 * @author suliwei
 * @description
 * @date 2025/5/26 14:31
 */
public class Request<T> {

    /**
     * 默认请求成功状态码
     */
    public static final Integer STATUS_SUCCESS = 0;

    /**
     * 默认请求失败状态码<br>
     * 通常导致请求失败的原因为可预知或可控的异常, 例如: 用户参数错误, 请求的资源不存在等
     */
    public static final Integer STATUS_FAILED = 1;

    /**
     * 默认请求成功响应信息
     */
    public static final String MESSAGE_SUCCESS = "Success";

    /**
     * 响应状态码
     */
    private Integer status;

    /**
     * 响应信息
     */
    private String message;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 响应数据
     */
    private T data;

    public Request(Integer status, String message) {
        checkArgument(status, message);
        this.status = status;
        this.message = message;
    }

    public Request(Integer status, String message, T data) {
        checkArgument(status, message);
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public static <T> Request<T> success(T data) {
        return new Request<>(STATUS_SUCCESS, MESSAGE_SUCCESS, data);
    }

    public static <T> Request<T> response(Integer status, String message, T data) {
        return new Request<>(status, message, data);
    }

    public static <T> Request<T> response(Integer status, String message) {
        return new Request<>(status, message);
    }

    private void checkArgument(Integer status, String message) {
        if (status == null) {
            throw new IllegalArgumentException("status must not be null");
        }

        if (StringUtils.isEmpty(message)) {
            throw new IllegalArgumentException("message must not be null");
        }
    }
}
